<template>
	<view>
		<zy-table :headList="headList" :dataList="dataList" :loading="loading" checkbox
			@handleSelectionChange="handleSelectionChange">
		</zy-table>
		<!-- <scroll-view scroll-y="true" style="height: 93vh;" scroll-with-animation="true"> -->

		<!-- <zy-table :show-header="true" :columns="column" :stripe="true" :data="contents"
				@toggleRowSelection="toggleRowSelection" @toggleAllSelection="toggleAllSelection"></zy-table> -->
		<!-- </scroll-view> -->
		<bottomBtn fixed :leftText="$t('public.reset')" :rightText="$t('public.submit')" @rightBtn="submitBtn" @leftBtn="resetBtn">
		</bottomBtn>
		<!-- 弹出层 -->
		<u-popup :show="show" :mode="popupData.mode" :round="popupData.round" :closeable="popupData.closeable"
			@close="close">
			<view class="u-popup-slot" style="height: 42vh;">
				<u--text :text="$t('homepage.WorkReport.completedQuantity')"
					customStyle="font-size:16px;font-weight:800;margin:15px auto;"></u--text>
				<view style="margin:25px 60px;display: flex;">
					<u-icon name="bookmark-fill" color="red" size="30"></u-icon>
					<u--text :text="$t('homepage.WorkReport.num')+$t('public.colon')"
						customStyle="font-size:18px;"></u--text>
					<zy-number-box :value="num" @change="valChange" color="#fff" bgcolor="#409efe" discolor="#fff"
						disbgcolor="#9AC9F9" size="16" integer>
					</zy-number-box>
					<!-- <u-number-box v-model="num" :min="0" @change="valChange" integer>
					</u-number-box> -->
				</view>
				<u-button type="primary" :text="$t('public.sure')" :disabled="disabled"
					customStyle="bottom:5px;margin:0 5px;position:fixed;border-radius:50px;width:97%;" @click="sure">
				</u-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import bottomBtn from "@/components/bottomBtn/index.vue"
	import {
		handSaveTaskReport,
	} from "@/api/homepage.js"
	export default {
		components: {
			bottomBtn
		},
		data() {
			// {
			// 	name: this.$t('homepage.WorkReport.no'),
			// 	key: 'productionNo',
			// },
			return {
				headList: [{
					name: this.$t('public.styleNo'),
					key: 'styleCode',
				}, {
					name: this.$t('public.procedureName'),
					key: 'procedureName',
				}, {
					name: this.$t('public.kzNum'),
					key: 'perBindNum',
				}, {
					name: this.$t('public.color'),
					key: 'color',
				}, {
					name: this.$t('public.size'),
					key: 'size',
				}, {
					name: this.$t('public.styleName'),
					key: 'styleName',
				}],
				dataList: [],
				num: 0,
				loading: true,
				// 弹出层
				popupData: {
					round: 10,
					mode: 'bottom',
					closeable: true,
				},
				show: false,
				changeChecked: '',
				changeCheckedLength: '',
				totalNum: '', //选中总计数量
				numListData: [],
				disabled: true
			}
		},
		onLoad: function(options) {
			this.dataList = []
			this.reportUserId = options.id //汇报人的id
			this.billUserName = options.name //汇报人名称
			this.productionTaskId = options.productionTaskId
			this.cuttingSheetId = options.cuttingSheetId
			this.$nextTick(() => {
				this.dataList = JSON.parse(options.data) //表格获取store数据
				this.loading = false
			})
		},
		//下拉刷新
		onPullDownRefresh() {
			setTimeout(function() {
				uni.redirectTo({
					url: "/pages/HomePage/WorkReport/tableList"
				})
				uni.stopPullDownRefresh();
			}, 1000);
		},
		methods: {
			// 点击多选框
			handleSelectionChange(e) {
				this.changeChecked = e.value
			},
			//提交
			submitBtn() {
				if (this.dataList.length > 0) {
					this.show = true
				} else {
					uni.$u.toast("暂无数据")
				}
			},
			//重置
			resetBtn() {
				uni.navigateBack({
					delta: 1
				});
			},
			//弹出层关闭
			close() {
				this.show = false
				this.num = 0
				this.disabled = true
			},
			//弹出层中数量
			valChange(e) {
				let tableNum = 0
				if (this.changeChecked && this.changeChecked.length > 0) {
					for (let i = 0; i < this.changeChecked.length; i++) {
						tableNum += this.changeChecked[i].perBindNum
					}
				} else {
					for (let i = 0; i < this.dataList.length; i++) {
						tableNum += this.dataList[i].perBindNum
					}
				}
				
				if (e > tableNum) {
					uni.$u.toast("不可超过剩余数量，请重新输入")
					this.disabled = true
				} else {
					this.disabled = false
					this.num = e
				}
			},
			//弹出层中确认
			sure() {
				this.id = this.$store.state.loginData.id
				let forLength = 1
				this.numListData = []
				if (this.changeChecked && this.changeChecked.length > 0) {
					if (this.changeChecked.length == 1) {
						for (var i = 0; i < forLength; i++) {
							let numList = {
								equipmentId: this.id,
								cardId: this.changeChecked[i].cardId,
								procedureName: this.changeChecked[i].procedureName,
								procedureId: this.changeChecked[i].procedureId,
								procedureCode: this.changeChecked[i].procedureNo,
								num: this.num,
								cuttingSheetId: this.cuttingSheetId,
								productionTaskId: this.productionTaskId,
								reportUserId: this.reportUserId,
								billUserName: this.billUserName,
								procedureIdList: this.changeChecked[i].procedureId
							}
							
							this.numListData.push(numList)
						}
					} else {
						let addNum = 0, addIndex = 0, addTotal = 0, lastNum = 0
						for (let i = 0; i < this.changeChecked.length; i++) {
							addNum += this.changeChecked[i].perBindNum
							addIndex++
							if (this.num - addNum <= 0) {
								break
							}
						}
						
						for (let i = 0; i < addIndex - 1; i++) {
							addTotal += this.changeChecked[i].perBindNum
						}
						
						forLength = addIndex
						lastNum = this.num - addTotal
						
						if (lastNum == 0) {
							for (var i = 0; i < forLength; i++) {
								let numList = {
									equipmentId: this.id,
									cardId: this.changeChecked[i].cardId,
									procedureName: this.changeChecked[i].procedureName,
									procedureId: this.changeChecked[i].procedureId,
									procedureCode: this.changeChecked[i].procedureNo,
									num: this.changeChecked[i].perBindNum,
									cuttingSheetId: this.cuttingSheetId,
									productionTaskId: this.productionTaskId,
									reportUserId: this.reportUserId,
									billUserName: this.billUserName,
									procedureIdList: this.changeChecked[i].procedureId
								}
								
								this.numListData.push(numList)
							}
						} else {
							for (var i = 0; i < forLength; i++) {
								let numList
								if (i < forLength - 1) {
									numList = {
										equipmentId: this.id,
										cardId: this.changeChecked[i].cardId,
										procedureName: this.changeChecked[i].procedureName,
										procedureId: this.changeChecked[i].procedureId,
										procedureCode: this.changeChecked[i].procedureNo,
										num: this.changeChecked[i].perBindNum,
										cuttingSheetId: this.cuttingSheetId,
										productionTaskId: this.productionTaskId,
										reportUserId: this.reportUserId,
										billUserName: this.billUserName,
										procedureIdList: this.changeChecked[i].procedureId
									}
								} else {
									numList = {
										equipmentId: this.id,
										cardId: this.changeChecked[i].cardId,
										procedureName: this.changeChecked[i].procedureName,
										procedureId: this.changeChecked[i].procedureId,
										procedureCode: this.changeChecked[i].procedureNo,
										num: lastNum,
										cuttingSheetId: this.cuttingSheetId,
										productionTaskId: this.productionTaskId,
										reportUserId: this.reportUserId,
										billUserName: this.billUserName,
										procedureIdList: this.changeChecked[i].procedureId
									}
								}
								
								this.numListData.push(numList)
							}
						}
					}
					
					handSaveTaskReport({
						taskReportArray: JSON.stringify(this.numListData)
					}).then((res) => {
						console.log(res)
						if (res.code == 0) {
							this.resetBtn()
							uni.$u.toast(res.message)
						} else {
							uni.$u.toast(res.message)
						}
					})
				} else {
					if (this.dataList.length == 1) {
						for (var i = 0; i < forLength; i++) {
							let numList = {
								equipmentId: this.id,
								cardId: this.dataList[i].cardId,
								procedureName: this.dataList[i].procedureName,
								procedureId: this.dataList[i].procedureId,
								procedureCode: this.dataList[i].procedureNo,
								num: this.num,
								cuttingSheetId: this.cuttingSheetId,
								productionTaskId: this.productionTaskId,
								reportUserId: this.reportUserId,
								billUserName: this.billUserName,
								procedureIdList: this.dataList[i].procedureId
							}
							
							this.numListData.push(numList)
						}
					} else {
						
						let addNum = 0, addIndex = 0, addTotal = 0, lastNum = 0
						for (let i = 0; i < this.dataList.length; i++) {
							addNum += this.dataList[i].perBindNum
							addIndex++
							if (this.num - addNum <= 0) {
								break
							}
						}
						
						for (let i = 0; i < addIndex - 1; i++) {
							addTotal += this.dataList[i].perBindNum
						}
						
						forLength = addIndex
						lastNum = this.num - addTotal
						
						if (lastNum == 0) {
							for (var i = 0; i < forLength; i++) {
								let numList = {
									equipmentId: this.id,
									cardId: this.dataList[i].cardId,
									procedureName: this.dataList[i].procedureName,
									procedureId: this.dataList[i].procedureId,
									procedureCode: this.dataList[i].procedureNo,
									num: this.dataList[i].perBindNum,
									cuttingSheetId: this.cuttingSheetId,
									productionTaskId: this.productionTaskId,
									reportUserId: this.reportUserId,
									billUserName: this.billUserName,
									procedureIdList: this.dataList[i].procedureId
								}
								
								this.numListData.push(numList)
							}
						} else {
							for (var i = 0; i < forLength; i++) {
								let numList
								if (i < forLength - 1) {
									numList = {
										equipmentId: this.id,
										cardId: this.dataList[i].cardId,
										procedureName: this.dataList[i].procedureName,
										procedureId: this.dataList[i].procedureId,
										procedureCode: this.dataList[i].procedureNo,
										num: this.dataList[i].perBindNum,
										cuttingSheetId: this.cuttingSheetId,
										productionTaskId: this.productionTaskId,
										reportUserId: this.reportUserId,
										billUserName: this.billUserName,
										procedureIdList: this.dataList[i].procedureId
									}
								} else {
									numList = {
										equipmentId: this.id,
										cardId: this.dataList[i].cardId,
										procedureName: this.dataList[i].procedureName,
										procedureId: this.dataList[i].procedureId,
										procedureCode: this.dataList[i].procedureNo,
										num: lastNum,
										cuttingSheetId: this.cuttingSheetId,
										productionTaskId: this.productionTaskId,
										reportUserId: this.reportUserId,
										billUserName: this.billUserName,
										procedureIdList: this.dataList[i].procedureId
									}
								}
								
								this.numListData.push(numList)
							}
						}
					}
					
					handSaveTaskReport({
						taskReportArray: JSON.stringify(this.numListData)
					}).then((res) => {
						console.log(res)
						if (res.code == 0) {
							this.resetBtn()
							uni.$u.toast(res.message)
						} else {
							uni.$u.toast(res.message)
						}
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
</style>